<template>
  <div class="upload_img">
    <el-row :gutter="24">
      <el-upload
        class="avatar-uploader"
        :action="request_url"
        :show-file-list="false"
        :on-success="handleAvatarSuccess"
        :before-upload="beforeAvatarUpload"
      >
        <img v-if="imageUrl" :src="imageUrl" class="avatar" />
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
    </el-row>
  </div>
</template>

<script>
export default {
  props: {
    value: {
      require: true
    },
    // 通过 component 中 props 字段传过来的数据，此字段需要先在 component 中配置
    myProps: {
      default: null
    }
  },
  computed: {
    imageUrl () {
      return this.value;
    },
    request_url() {
      return this.myProps;
    }
  },
  data() {
    return {};
  },
  methods: {
    // 上传成功获取图片放到上面
    handleAvatarSuccess(res, file) {
      this.value = URL.createObjectURL(file.raw);
      this.$message({
        message: "图片上传成功,请点击确定",
        type: "success"
      });
      // 获取 URL 的协议部分 http:
      let protocol = window.location.protocol;
      // 获取与 URL 关联的端口号
      let port = window.location.port;
      //获取当前窗口的主机名
      let host = window.location.host;
      let netAdd = `${protocol}//${host}${process.env.VUE_APP_API}/image/${res.rows}`;
      sessionStorage.imgUrl = netAdd;
      console.log(sessionStorage.imgUrl);
    },

    // 点击上传图片
    beforeAvatarUpload(file) {
      return;
    }
  }
};
</script>

<style lang="scss">
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  background: #E8E8E8;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>